<template>
  <div class="container">
    <el-form ref="form" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="ruleForm.title" />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <editor v-model="ruleForm.articleBody" @blur="articleBodyBlur" />
      </el-form-item>
      <el-form-item label="视频地址">
        <el-input v-model="ruleForm.videoURL" />
      </el-form-item>
    </el-form>
    <div style="margin-top: 50px;text-align: right">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import editor from './editor.vue'
import { add, update } from '@/api/hmmm/articles.js'

export default {
  components: { editor },
  props: {
    formData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      ruleForm: {
        id: null,
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    if (this.formData.id) {
      this.ruleForm.id = this.formData.id
      this.ruleForm.title = this.formData.title
      this.ruleForm.articleBody = this.formData.articleBody
      this.ruleForm.videoURL = this.formData.videoURL
    }
  },
  methods: {
    articleBodyBlur() {
      this.$refs.form.validateField('articleBody')
    },
    closeDialog() {
      this.$emit('close')
    },
    async hSubmit() {
      if (this.formData.id) {
        await this.doModify()
      } else {
        await this.doAdd()
      }
      this.closeDialog()
    },
    async doAdd() {
      try {
        const res = await add({ title: this.ruleForm.title, articleBody: this.ruleForm.articleBody, videoURL: this.ruleForm.videoURL })
        console.log(res)
        this.$emit('submit')
      } catch (error) {
        console.dir(error)
      }
    },
    async doModify() {
      try {
        const res = await update(this.ruleForm)
        console.log(res)
        this.$emit('submit')
      } catch (error) {
        console.dir(error)
      }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .ql-editor{
  height: 200px!important;
}
</style>
